library(tidyverse)
library(readxl)
library(openintro) ## for converting state abbreviations
library(mgsub)
source('lib/fix-names.R')
options(scipen=0, digits=7) #to standardize number of digits when exporting data. all data here is run using these options.

##ECONOMIC CONTROLS ---------------
LESO_path <- "raw-data/LESO-Controls"
tempcityeconomic = list.files(path = LESO_path,pattern="citypovertyincome20[0-9][0-9].csv")
tempcityeconomic <- lapply(file.path(LESO_path,tempcityeconomic), read_csv)

econ <- tempcityeconomic %>%
  plyr::rbind.fill(.) %>%
  select(Id2,state,city,year,unemployment,medianhouseholdincome,percentinpoverty,PCPI) %>%
  mutate(medianhouseholdincome = mgsub::mgsub(medianhouseholdincome, 
                                              c("X", "x",",","\\+","-"), 
                                              c(NA, NA,"","",""))) %>%
  na_if(.,"-") %>% 
  mutate_at(c('unemployment',"medianhouseholdincome","percentinpoverty","PCPI"),
          as.numeric)

rm(tempcityeconomic)    

save(econ, file="data/econ.RData")

#Economic data by county -----------
tempcountyeconomic = list.files(path = LESO_path,pattern="countypovertyincome20[0-9][0-9].csv")
econ <- lapply(file.path(LESO_path,tempcountyeconomic), read_csv)

countyecon <- econ %>%
  plyr::rbind.fill(.) %>%
  select(county,state,unemployment,medianhouseholdincome,percentinpoverty,year,PCPI)%>%
  mutate(state_abbrv = state2abbr(state))

rm(tempcountyeconomic) 

# fix county names -------
##finally, need to make sure variables merge correctly with County
louisiana <- which(countyecon$state_abbrv=='LA')
alaska <- which(countyecon$state_abbrv=='AK')

countyecon$county[countyecon$county=='LaSalle' & countyecon$state_abbrv=='LA'] <- "La Salle"
countyecon$county[louisiana] <- paste(countyecon$county[louisiana],"Parish",sep=" ")

dona_ana <- grep('Ana',countyecon$county,ignore.case = F)
nm <- which(countyecon$state=='New Mexico')
dona_ana <- dona_ana[dona_ana %in% nm] #keep only Dona Ana in New Mexico

countyecon$county[dona_ana] <- "Dona Ana"

countyecon$county[countyecon$county=="Oglala Lakota"] <- "Shannon"
countyecon$county[countyecon$county=="Petersburg Borough"] <- "Petersburg Census Area"
countyecon$county[countyecon$county=="Kusilvak Census Area"] <- "Wade Hampton Census Area" #name change

countyecon$alt_county_name <- countyecon$county

## Louisiana and Alaska do not want to add the word county to name
omit <- c("District of Columbia","Baltimore city","St. Louis city","Carson City")
do_not_change <- sort(c(louisiana,alaska,which(countyecon$county %in% omit)))
countyecon$alt_county_name[-do_not_change] <- paste(countyecon$county[-do_not_change], "County")

countyecon$alt_county_name <- fix_VA_counties(countyecon$alt_county_name)

# Load in fips codes ---------
countyecon <- read_csv("raw-Data/countyfips.csv") %>%
  mutate(state.fips = str_pad(state.fips,width=2,pad=0,side="left"),
         county.fips = str_pad(county.fips,width=2,pad=0,side="left")) %>%
  left_join(countyecon,.,by = c("alt_county_name"="County",
                                "state_abbrv"="State")) %>%
  select(-county) # so can merge with demog data don't need this extra name

save(countyecon,file='data/econ_county.RData')

